import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'welcome',
      component: () => import('@/views/chat/index.vue'),
      meta: {
        tabbar: true,
        title: '玄武町'
      }
    },
    {
      path: '/new-friend',
      name: 'new-friend',
      component: () => import('@/views/new-friend/index.vue'),
      meta: {
        tabbar: true,
        title: '新朋友'
      }
    },
    {
      path: '/account',
      name: 'account',
      component: () => import('../views/account'),
      meta: {
        tabbar: true,
        title: '我的账户'
      }
    },
    {
      path: '/rooms',
      name: 'rooms',
      component: () => import('../views/rooms'),
      meta: {
        title: '讨论组'
      }
    },
    {
      path: '/rooms/:roomId',
      name: 'room-chat',
      component: () => import('../views/rooms/chat.vue'),
      meta: {
        title: '讨论组'
      }
    },
    {
      path: '/rooms/:roomId/detail',
      name: 'room-detail',
      component: () => import('../views/rooms/detail.vue'),
      meta: {
        title: '讨论组'
      }
    },
    {
      path: '/lives',
      name: 'lives',
      component: () => import('../views/lives'),
      meta: {
        tabbar: true,
        title: '我的账户'
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/account/login'),
      meta: {
        title: '账户登录'
      }
    },
    {
      path: '/chat',
      name: 'chat',
      component: () => import('../views/chat/index.vue'),
      meta: {
        tabbar: true,
        title: '聊天'
      }
    },
    {
      path: '/new-chat',
      name: 'new-chat',
      component: () => import('@/views/chat/index.vue'),
      meta: {
        tabbar: true,
        title: '聊天'
      }
    },
    {
      path: '/contacts',
      name: 'contacts',
      component: () => import('@/views/contact/index.vue'),
      meta: {
        tabbar: true,
        title: '联系人'
      }
    },
    {
      path: '/chatItem/:partnerId',
      name: 'chatItem',
      component: () => import('../views/chat-item')
    },
    {
      path: '/user-search',
      name: 'user-search',
      component: () => import('@/views/search/index.vue')
    },
    {
      path: '/chat-item/:partnerId',
      name: 'chat-item',
      component: () => import('@/views/chat-item/index.vue')
    },
    {
      path: '*',
      component: () => import('../views/v404')
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
